//
//  SAShopBatchVC.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/27.
//

#import "SAShopBatchVC.h"
#import "SAProductOverModel.h"
#import "SAShopBatchCell.h"
#import "HDTabBarItem.h"
#import "GFAlertView.h"
#import "SABatchUpperOrLowerAPI.h"
#import "SAUpdateInventoryView.h"
#import "SAUpdateShopPriceView.h"
#import "SAProductBatchPriceAPI.h"
#import "SAProductBatchIsDeleteAPI.h"
#import "SAProductListAPI.h"
#import "SAInventorySortView.h"
#import "SAOrgProductImportMerchantProductAAPI.h"
#import "SAUpdateDiscountView.h"
#import "SAProductBatchUpdateItemizeAPI.h"
#import "SAProductChooseSeasonView.h"

#define kTabBarTitle @[@"上架",@"下架",@"调价",@"统一折扣率",@"批量调整类别",@"预警",@"删除",@"取消"]
#define kTabBarImage @[@"shop_shelves",@"shop_downShelves",@"shop_price",@"shop_warn",@"shop_delete"]

@interface SAShopBatchVC ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate>

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSMutableArray<SAProductOverModel *> *dataMarr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SABatchUpperOrLowerAPI *batchUpperOrLowerAPI;
@property (nonatomic, strong) SAProductBatchPriceAPI *productBatchPriceAPI;
@property (nonatomic, strong) SAProductBatchIsDeleteAPI *productBatchIsDeleteAPI;
@property (nonatomic, strong) NSMutableArray<SAProductOverModel *> *selectMarr;

@property (nonatomic, strong) SAProductListAPI *productListAPI;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) UILabel *selectLab;
@property (nonatomic, strong) UIButton *exportBtn;
@property (nonatomic, strong) UIButton *batchBtn;

@property (nonatomic, strong) SAOrgProductImportMerchantProductAAPI *orgProductImportMerchantProductAAPI;
@property (nonatomic, strong) SAProductBatchUpdateItemizeAPI *productBatchUpdateItemizeAPI;
@property (nonatomic, strong) SAProductChooseColorList *typeModel;//选择类别

@end

@implementation SAShopBatchVC

-(instancetype)initWithProductListAPI:(SAProductListAPI *)api {
    if (self = [super init]) {
        self.productListAPI.attrIds = api.attrIds;
        self.productListAPI.currentPage = 1;
        self.productListAPI.order = api.order;
        self.productListAPI.pageSize = 20;
        self.productListAPI.productId = api.productId;
        self.productListAPI.productName = api.productName;
        self.productListAPI.providerId = api.providerId;
        self.productListAPI.state = api.state;
        self.productListAPI.unitId = api.unitId;
        self.productListAPI.merchantId = api.merchantId;
        
        self.dataMarr = [NSMutableArray array];
        self.selectMarr = [NSMutableArray array];
        [self.tableView reloadData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.batchUpperOrLowerAPI == api) {
        if ([self.batchUpperOrLowerAPI.type isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"批量上架成功" duration:2];
        }else {
            [Prompt popPromptViewWithMsg:@"批量下架成功" duration:2];
        }
        !self.refreshBlock ?: self.refreshBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }else if (self.productBatchPriceAPI == api) {
        [Prompt popPromptViewWithMsg:@"已调整所选商品价格" duration:2];
        !self.refreshBlock ?: self.refreshBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }else if (self.productBatchIsDeleteAPI == api) {
        [Prompt popPromptViewWithMsg:@"已删除所选商品" duration:2];
        !self.refreshBlock ?: self.refreshBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }else if (self.productListAPI == api) {
        if (self.currentPage == 1) {
            self.dataMarr = [SAProductOverModel mj_objectArrayWithKeyValuesArray:data[@"data"][@"records"]];
            self.selectBtn.selected = NO;
            [self.selectMarr removeAllObjects];
            self.title = [NSString stringWithFormat:@"已选%zd项",self.selectMarr.count];
        }else {
            for (NSDictionary *dict in (NSArray *)data[@"data"][@"records"]) {
                SAProductOverModel *model = [SAProductOverModel mj_objectWithKeyValues:dict];
                [self.dataMarr addObject:model];
            }
        }
        self.selectLab.text = [NSString stringWithFormat:@"全选本页\n(%zd)",self.dataMarr.count];
        if (self.selectBtn.isSelected == YES) {
            [self.selectMarr removeAllObjects];
            for (SAProductOverModel *model in self.dataMarr) {
                model.isSelected = YES;
                [self.selectMarr addObject:model];
            }
            self.title = [NSString stringWithFormat:@"已选%zd项",self.selectMarr.count];
        }
        
        [self.tableView reloadData];
    }else if (self.orgProductImportMerchantProductAAPI == api) {
        [Prompt popPromptViewWithMsg:@"已导入所选商品" duration:2];
    }else if (self.productBatchUpdateItemizeAPI) {
        [Prompt popPromptViewWithMsg:@"批量调整类别成功" duration:2];
        !self.refreshBlock ?: self.refreshBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    SAShopBatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAShopBatchCell" forIndexPath:indexPath];
    SAProductOverModel *model = self.dataMarr[indexPath.row];
    cell.model = model;
    cell.selectBlock = ^(BOOL isSelect) {
        model.isSelected = isSelect;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if (isSelect == YES) {
            [weakSelf.selectMarr addObject:model];
        }else {
            [weakSelf.selectMarr removeObject:model];
        }
        if (weakSelf.selectMarr.count ==  weakSelf.dataMarr.count) {
            weakSelf.selectBtn.selected = YES;
        }else {
            weakSelf.selectBtn.selected = NO;
        }
        weakSelf.title = [NSString stringWithFormat:@"已选%zd项",weakSelf.selectMarr.count];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(71);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return SafeFooterHeight;
}

#pragma mark - event
-(void)clickedSelectBtn {
    self.selectBtn.selected = !self.selectBtn.selected;
    [self.selectMarr removeAllObjects];
    for (SAProductOverModel *model in self.dataMarr) {
        model.isSelected = self.selectBtn.isSelected;
        if (self.selectBtn.isSelected == YES) {
            [self.selectMarr addObject:model];
        }
    }
    self.title = [NSString stringWithFormat:@"已选%zd项",self.selectMarr.count];
    [self.tableView reloadData];
}

- (void)selectedItem:(NSInteger)tag {
    if (self.selectMarr.count == 0) {
        [Prompt popPromptViewWithMsg:@"至少选中一条数据!" duration:2];
        return;
    }
    if (tag == 100) {
        GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"确定要将所有的商品上架?" knowBtnBlock:^(UIButton *knowBtn) {
            [knowBtn addTarget:self action:@selector(clickedShopShelves) forControlEvents:UIControlEventTouchUpInside];
        }];
        [alertView showWithAnimation];
    }else if (tag == 101) {
        GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"确定要将所有的商品下架?" knowBtnBlock:^(UIButton *knowBtn) {
            [knowBtn addTarget:self action:@selector(clickedShopDownShelves) forControlEvents:UIControlEventTouchUpInside];
        }];
        [alertView showWithAnimation];
    }else if (tag == 102) {
        [self clickedIUpdateShopPriceView];
    }else if (tag == 103) {
        [self clickedDiscountView];
    }else if (tag == 104) {
        [self typeChooseBtnClick];
    }else if (tag == 105) {
        [self clickedInventoryView];
    }else if (tag == 106) {
        NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
        NSString *productViewDelete = [NSString stringWithFormat:@"%@",menuList[1][@"perms"][@"productViewDelete"]];
        if ([productViewDelete isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"您没有删除商品的权限,请联系店长添加" duration:2];
        }else {
            GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"确定要删除所选商品?" knowBtnBlock:^(UIButton *knowBtn) {
                [knowBtn addTarget:self action:@selector(clickedDeleteShop) forControlEvents:UIControlEventTouchUpInside];
            }];
            [alertView showWithAnimation];
        }
    }
}

//商品上架
-(void)clickedShopShelves {
    NSMutableArray *marr = [NSMutableArray array];
    for (SAProductOverModel *model in self.selectMarr) {
        [marr addObject:@(model.productId)];
    }
    self.batchUpperOrLowerAPI.type = @"1";
    self.batchUpperOrLowerAPI.coverParameters = @{@"ids" : marr};
    [self.batchUpperOrLowerAPI sendRequestToServerWithActivityViewAndMask:POST];
}

//商品下架
-(void)clickedShopDownShelves {
    NSMutableArray *marr = [NSMutableArray array];
    for (SAProductOverModel *model in self.selectMarr) {
        [marr addObject:@(model.productId)];
    }
    self.batchUpperOrLowerAPI.type = @"0";
    self.batchUpperOrLowerAPI.coverParameters = @{@"ids" : marr};
    [self.batchUpperOrLowerAPI sendRequestToServerWithActivityViewAndMask:POST];
}

//调价
-(void)clickedIUpdateShopPriceView {
    SAUpdateShopPriceView *shopPriceView = [SAUpdateShopPriceView shareCornView];
    shopPriceView.selectMarr = self.selectMarr;
    [shopPriceView showView:self];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    WeakSelf
    shopPriceView.pricinSuccessBlock = ^(NSString * _Nonnull wholesalePriceStr, NSString * _Nonnull retailPrice, NSString * _Nonnull merchantPrice, NSString * _Nonnull type) {
        for (SAProductOverModel *model in weakSelf.selectMarr) {
            if (model.isSelected == YES) {
                [array addObject:@(model.productId)];
            }
        }
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        dict[@"ids"] = array;
        dict[@"type"] = @([type integerValue]);
        if (![wholesalePriceStr isEqualToString:@""]) {
            dict[@"wholesalePrice"] = [NSString stringWithFormat:@"%ld",[wholesalePriceStr integerValue] *100];
        }
        if (![retailPrice isEqualToString:@""]) {
            dict[@"retailPrice"] = [NSString stringWithFormat:@"%ld",[retailPrice integerValue] *100];
        }
        if (![merchantPrice isEqualToString:@""]) {
            dict[@"merchantPrice"] = [NSString stringWithFormat:@"%ld",[merchantPrice integerValue] *100];
        }
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认调整所选商品价格？" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
            self.productBatchPriceAPI.coverParameters = dict;
            [self.productBatchPriceAPI sendRequestToServerWithActivityViewAndMask:POST];
        }];
        UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [weakSelf presentViewController:alert animated:YES completion:nil];
    };
    
    shopPriceView.pricingSuccessBlock = ^(NSString * _Nonnull str, NSString * _Nonnull type) {
        for (SAProductOverModel *model in weakSelf.selectMarr) {
            if (model.isSelected == YES) {
                [array addObject:@(model.productId)];
            }
        }
        
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        dict[@"ids"] = array;
        dict[@"type"] = @([type integerValue]);
        if ([type isEqualToString:@"2"]) {
            dict[@"plusReduce"] = [NSString stringWithFormat:@"%ld",[str integerValue] *100];
        }else
        {
            dict[@"ride"] = str;
        }
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认调整所选商品价格？" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
            self.productBatchPriceAPI.coverParameters = dict;
            [self.productBatchPriceAPI sendRequestToServerWithActivityViewAndMask:POST];
        }];
        UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [weakSelf presentViewController:alert animated:YES completion:nil];
    };
}

//统一调价
-(void)clickedDiscountView {
    SAUpdateDiscountView *discountView = [SAUpdateDiscountView shareCornView];
    discountView.selectMarr = self.selectMarr;
    [discountView showView:self];
    WeakSelf
    discountView.refreshBlock = ^{
        !weakSelf.refreshBlock ?: weakSelf.refreshBlock();
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
}

//预警
-(void)clickedInventoryView {
    SAUpdateInventoryView *inventoryView = [SAUpdateInventoryView shareCornView];
    inventoryView.selectMarr = self.selectMarr;
    [inventoryView showView:self];
    WeakSelf
    inventoryView.refreshBlock = ^{
        !weakSelf.refreshBlock ?: weakSelf.refreshBlock();
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
}

//删除
-(void)clickedDeleteShop{
    NSMutableArray *marr = [NSMutableArray array];
    for (SAProductOverModel *model in self.selectMarr) {
        [marr addObject:@(model.productId)];
    }
    self.productBatchIsDeleteAPI.coverParameters = @{@"ids" : marr};
    [self.productBatchIsDeleteAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(void)clickedExportBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *importProduct = [NSString stringWithFormat:@"%@",menuList[1][@"perms"][@"importProduct"]];
    if ([importProduct isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有导入商品权限,请联系店长添加" duration:2];
        return;;
    }
    
    if (self.selectMarr.count == 0) {
        [Prompt popPromptViewWithMsg:@"请选择需要导入的商品" duration:2];
        return;
    }
    NSMutableArray *marr = [NSMutableArray array];
    for (SAProductOverModel *model in self.selectMarr) {
        [marr addObject:@(model.productId)];
    }
    self.orgProductImportMerchantProductAAPI.sourceMerchantId = self.productListAPI.merchantId;
    self.orgProductImportMerchantProductAAPI.coverParameters = @{@"productIds" : marr};
    [self.orgProductImportMerchantProductAAPI sendRequestToServerWithType:POST];
}

-(void)clickedBatchBtn {
    SAInventorySortView *sortView = [SAInventorySortView shareCornView];
    [sortView loadData:kTabBarTitle AndSelectNum:-1];
    UIViewController *vc = nil;
    [sortView showView:vc];
    WeakSelf
    sortView.selectBlock = ^(NSInteger selectNum) {
        [weakSelf selectedItem:selectNum + 100];
    };
}

//类别选择
-(void)typeChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:[NSMutableArray array] andType:@"4" andIsDetail:@"1" andIsDetailId:-1];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        SAProductChooseColorList *selectModel = nil;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                selectModel = list;
            }
        }
        if (selectModel != nil) {
            weakSelf.typeModel = selectModel;
            GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"确定要批量修改所选商品类别?" knowBtnBlock:^(UIButton *knowBtn) {
                [knowBtn addTarget:self action:@selector(confirmTypeChooseBtnClick) forControlEvents:UIControlEventTouchUpInside];
            }];
            [alertView showWithAnimation];
        }
    };
}

-(void)confirmTypeChooseBtnClick {
    NSMutableArray *marr = [NSMutableArray array];
    for (SAProductOverModel *model in self.selectMarr) {
        [marr addObject:@(model.productId)];
    }
    NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
    mdic[@"productIds"] = marr;
    mdic[@"attrId"] = @(self.typeModel.attrId).stringValue;
    mdic[@"attrName"] = self.typeModel.attrName;
    mdic[@"attrType"] = @"4";
    self.productBatchUpdateItemizeAPI.coverParameters = mdic;
    [self.productBatchUpdateItemizeAPI sendRequestToServerWithActivityViewAndMask:POST];
}

#pragma mark - initUI
-(void)initUI {
    self.title = @"已选0项";
    [self setBgImage:nil];
    [self addRightViews:@[self.backBtn]];
    
    [self.view addSubview:self.tableView];
    [self initTabBar];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"已经到底啦~" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
    
    self.currentPage = 1;
    [self loadData];
}

-(void)loadData {
    self.productListAPI.currentPage = self.currentPage;
    [self.productListAPI sendRequestToServerWithActivityViewAndMask:POST];
}

- (void)initTabBar{
    
    //    CGFloat itemWidth, itemHieght;
    //    itemWidth = kScreenWidth / kTabBarTitle.count;
    //    itemHieght = 49;
    //    UIView *customBar = [[UIView alloc] init];
    //    [self.view addSubview:customBar];
    //
    //    if (KIsiPhoneX) {
    //        itemHieght = 49 + SafeFooterHeight/kScreenScale;
    //    }
    //    customBar.backgroundColor = [UIColor whiteColor];
    //
    //    [customBar mas_makeConstraints:^(MASConstraintMaker *make) {
    //        kMasLeft(0);kMasBottom(0);kMasWidth(kBaseScreenWidth);kMasHeight(itemHieght);
    //    }];
    //
    //    for (int i = 0; i < kTabBarTitle.count; i++) {
    //        HDTabBarItem *item = [HDTabBarItem tabBarItemWithTitle:kTabBarTitle[i] normalImg:kTabBarImage[i] selectImg:kTabBarImage[i]];
    //        item.tag = 100 + i;
    //        [item addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
    //        item.frame = CGRectMake(itemWidth * i, 0, itemWidth, kScaleNum(49));
    //        [customBar addSubview:item];
    //        item.selected = YES;
    //        item.backgroundColor = [UIColor whiteColor];
    //    }
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(45) - SafeFooterHeight, kScreenWidth, kScaleNum(45) + SafeFooterHeight)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    [bottomView addSubview:self.selectLab];
    [bottomView addSubview:self.selectBtn];
    if ([SAValueManger shareInStance].userInfo.merchantId == [self.productListAPI.merchantId integerValue]) {
        [bottomView addSubview:self.batchBtn];
        
    }else {
        [bottomView addSubview:self.exportBtn];
    }
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 1)];
    line.backgroundColor = kLineColor;
    [bottomView addSubview:line];
}

#pragma mark - lazy
-(UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_backBtn setTitle:@"取消" forState:UIControlStateNormal];
        _backBtn.frame = SARectMake(254, 0, 80, 44);
        [_backBtn addTarget:self action:@selector(backToLastVC) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 45) - SafeFooterHeight) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAShopBatchCell class] forCellReuseIdentifier:@"SAShopBatchCell"];
    }
    return _tableView;
}

-(UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.frame = SARectMake(0, 0, 60, 45);
        [_selectBtn setImage:[UIImage imageNamed:@"shop_grayRound"] forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"shop_blueCheck"] forState:UIControlStateSelected];
        [_selectBtn addTarget:self action:@selector(clickedSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(UILabel *)selectLab {
    if (!_selectLab) {
        _selectLab = [[UILabel alloc] initWithFrame:SARectMake(60, 0, 70, 45)];
        _selectLab.textColor = kBlackColor;
        _selectLab.font = kFont(15);
        _selectLab.numberOfLines = 2;
        _selectLab.textAlignment = NSTextAlignmentCenter;
    }
    return _selectLab;
}

-(UIButton *)exportBtn {
    if (!_exportBtn) {
        _exportBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _exportBtn.frame = SARectMake(kBaseScreenWidth - 208, 0, 104, 45);
        _exportBtn.frame = SARectMake(kBaseScreenWidth - 104, 0, 104, 45);
        _exportBtn.backgroundColor = kColor(236, 239, 255, 1);
        _exportBtn.titleLabel.font = kFont(18);
        [_exportBtn setTitle:@"导入商品" forState:UIControlStateNormal];
        [_exportBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
        [_exportBtn addTarget:self action:@selector(clickedExportBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _exportBtn;
}

-(UIButton *)batchBtn {
    if (!_batchBtn) {
        _batchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _batchBtn.frame = SARectMake(kBaseScreenWidth - 104, 0, 104, 45);
        _batchBtn.backgroundColor = kBuleColor;
        _batchBtn.titleLabel.font = kFont(18);
        [_batchBtn setTitle:@"批量操作" forState:UIControlStateNormal];
        [_batchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_batchBtn addTarget:self action:@selector(clickedBatchBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _batchBtn;
}

-(SABatchUpperOrLowerAPI *)batchUpperOrLowerAPI {
    if (!_batchUpperOrLowerAPI) {
        _batchUpperOrLowerAPI = [SABatchUpperOrLowerAPI apiWithDelegate:self];
    }
    return _batchUpperOrLowerAPI;
}

-(SAProductBatchPriceAPI *)productBatchPriceAPI {
    if (!_productBatchPriceAPI) {
        _productBatchPriceAPI = [SAProductBatchPriceAPI apiWithDelegate:self];
    }
    return _productBatchPriceAPI;
}

-(SAProductBatchIsDeleteAPI *)productBatchIsDeleteAPI {
    if (!_productBatchIsDeleteAPI) {
        _productBatchIsDeleteAPI = [SAProductBatchIsDeleteAPI apiWithDelegate:self];
    }
    return _productBatchIsDeleteAPI;
}

-(SAProductListAPI *)productListAPI {
    if (!_productListAPI) {
        _productListAPI = [SAProductListAPI apiWithDelegate:self];
    }
    return _productListAPI;
}

-(SAOrgProductImportMerchantProductAAPI *)orgProductImportMerchantProductAAPI {
    if (!_orgProductImportMerchantProductAAPI) {
        _orgProductImportMerchantProductAAPI = [SAOrgProductImportMerchantProductAAPI apiWithDelegate:self];
    }
    return _orgProductImportMerchantProductAAPI;
}

-(SAProductBatchUpdateItemizeAPI *)productBatchUpdateItemizeAPI {
    if (!_productBatchUpdateItemizeAPI) {
        _productBatchUpdateItemizeAPI = [SAProductBatchUpdateItemizeAPI apiWithDelegate:self];
    }
    return _productBatchUpdateItemizeAPI;
}

@end
